package com.cheng.operation.impl;

import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.CodeUtil;
import com.cheng.common.utils.IdUtil;
import com.cheng.common.utils.RespUtils;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.CompanyMoveInfEntity;
import com.cheng.data.vo.*;
import com.cheng.operation.CompanyMoveInfOperation;
import com.cheng.operation.GvmtTypeInfOperation;
import com.cheng.service.AcceptInfoService;
import com.cheng.service.CompanyInfoService;
import com.cheng.service.CompanyMoveInfService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author: ChengJW
 * @date: 2021/12/18 10:50
 * @version: 1.0
 * @description: 企业迁移信息处理
 */
@Service
public class CompanyMoveInfOperationImpl implements CompanyMoveInfOperation {

    private final static Logger log = LoggerFactory.getLogger(CompanyMoveInfOperationImpl.class);

    @Autowired
    private CompanyMoveInfService companyMoveInfService;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private AcceptInfoService acceptInfoService;

    @Autowired
    private GvmtTypeInfOperation gvmtTypeInfOperation;

    /**
     * @author: ChengJW
     * @params: [vo]
     * @return: void
     * @date: 2021/12/18
     * @description: 企业迁出信息
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertCompanyMoveInf(CompanyMoveInfVO vo) throws CommonException{
        if (vo == null){
            log.error("企业迁移信息为空，无法进行下一步操作");
            throw new CommonException("CJW202111010056","CompanyMoveInfOperationImpl","insertCompanyMoveInf");
        }
        CompanyMoveInfEntity entity = new CompanyMoveInfEntity();
        BeanUtils.copyProperties(vo,entity);
        entity.setCmId(IdUtil.getCMId());
        entity.setCmMvOutDt(new Date());
        entity.setCmMvCd(CodeUtil.MOVE_STD_1);
        entity.setStDt(new Date());
        entity.setUtDt(new Date());
        companyMoveInfService.insertCompanyMoveInf(entity);

        if (StringUtil.isNotNull(vo.getCmCreditCode()) && StringUtil.isNotNull(vo.getCmMvInCode())) {
            //批量修改企业信息表的档案卷管辖机关代码跟管辖机关名称
            CompanyInfoVO companyInfoVO = new CompanyInfoVO();
            companyInfoVO.setCiCreditCode(vo.getCmCreditCode());
            companyInfoVO.setCiMgeCode(vo.getCmMvInCode());
            companyInfoService.updateCompanyInfo(companyInfoVO);

            //批量修改受理信息表的档案信息管辖机关代码
            AcceptInfoVO acceptInfoVO = new AcceptInfoVO();
            acceptInfoVO.setAtCreditCode(vo.getCmCreditCode());
            acceptInfoVO.setAtCode(vo.getCmMvInCode());
            acceptInfoService.updateBatchAcceptInfo(acceptInfoVO);
        }
    }

    /**
     * @author: ChengJW
     * @params: []
     * @return: com.cheng.data.vo.RespPageInfoVO
     * @date: 2021/12/18
     * @description: 查询迁移企业信息
     */
    @Override
    public RespPageInfoVO queryCompanyMoveList(ReqPageInfoVO vo) throws CommonException {

        if (null == vo){
            log.error("查询条件信息为空，无法进行下一步操作");
            throw new CommonException("CJW202111010054","CompanyMoveInfOperationImpl","queryCompanyMoveList");
        }
        Map<String,Object> map = new HashMap<>();
        map.put(CodeUtil.SRCH_REQ_FIR,vo.getSrchReqFir());
        if (StringUtil.isNotNull(String.valueOf(vo.getQryReqFir()))){
            map.put(CodeUtil.QRY_REQ_FIR,"%"+vo.getQryReqFir()+"%");
        }
        map.put(CodeUtil.USER_CODE,vo.getUserCode());
        PageInfo<CompanyMoveInfVO> pageInfo = PageHelper.startPage(vo.getPageNum(),vo.getPageSize()).doSelectPageInfo(() -> companyMoveInfService.queryCompanyMoveList(map));
        List<CompanyMoveInfVO> list = new ArrayList<>();
        if (pageInfo.getList() != null && pageInfo.getList().size() > 0){
            for (CompanyMoveInfVO companyMoveInfVO : pageInfo.getList()){
                companyMoveInfVO.setCmMvOutNm(gvmtTypeInfOperation.queryGvmtTypeInfNmByCode(companyMoveInfVO.getCmMvOutCode()));
                list.add(companyMoveInfVO);
            }
        }
        pageInfo.setList(list);
        return RespUtils.respPageInfoVO(pageInfo);
    }

    /**
    * @author: ChengJW
    * @params: [vo]
    * @return: void
    * @date: 2021/12/18
    * @description: 档案迁入
    */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCompanyMoveInf(CompanyMoveInfVO vo) throws CommonException {
        if (StringUtil.isNotNull(vo.getCmCreditCode())){
            vo.setCmMvCd(CodeUtil.MOVE_STD_0);
            companyMoveInfService.updateCompanyMoveInf(vo);

            //批量修改企业信息表的档案卷管辖机关代码跟管辖机关名称
            CompanyInfoVO companyInfoVO = new CompanyInfoVO();
            companyInfoVO.setCiCreditCode(vo.getCmCreditCode());
            companyInfoVO.setCiMgeCode(vo.getCmMvInCode());
            companyInfoVO.setCiMvCd(CodeUtil.MOVE_STD_1);
            companyInfoService.updateCompanyInfo(companyInfoVO);

            //批量修改受理信息表的档案信息管辖机关代码
            AcceptInfoVO acceptInfoVO = new AcceptInfoVO();
            acceptInfoVO.setAtCreditCode(vo.getCmCreditCode());
            acceptInfoVO.setAtCode(vo.getCmMvInCode());
            acceptInfoVO.setAtMvCd(CodeUtil.MOVE_STD_1);
            acceptInfoService.updateBatchAcceptInfo(acceptInfoVO);
        }
    }

    /**
    * @author: ChengJW
    * @params: [pageInfoVO]
    * @return: com.cheng.data.vo.RespPageInfoVO
    * @date: 2021/12/20
    * @description: 统计迁移情况
    */
    @Override
    public RespPageInfoVO queryStatisticsMvVOList(ReqPageInfoVO pageVO) throws CommonException {
        if (pageVO == null || pageVO.getSrchReqFir() == null){
            log.error("查询条件信息为空，无法进行下一步操作");
            throw new CommonException("CJW202111010054","CompanyInfoOperationImpl","queryStatisticsVOList");
        }

        //对查询结果进行分页
        Map<String,Object> map = new HashMap<>();
        map.put(CodeUtil.STATISTIC_CODE,pageVO.getSrchReqFir());
        //开始日期为当天的第一秒
        if (StringUtil.isNotNull(pageVO.getStDt())){
            map.put("stDt",pageVO.getStDt()+" 00:00:00");
        }
        if (StringUtil.isNotNull(pageVO.getLmDt())){
            map.put("lmDt",pageVO.getLmDt()+" 23:59:59");
        }
        PageInfo<StatisticsVO> pageInfo = PageHelper.startPage(pageVO.getPageNum(),pageVO.getPageSize()).doSelectPageInfo(() -> companyMoveInfService.queryStatisticsMvVOList(map));
        List<StatisticsVO> list = new ArrayList<>();
        if (pageInfo.getList() != null && pageInfo.getList().size() > 0){
            for (StatisticsVO vo : pageInfo.getList()){
                vo.setMgeNm(gvmtTypeInfOperation.queryGvmtTypeInfNmByCode(vo.getMgeCode()));
                list.add(vo);
            }
        }
        pageInfo.setList(list);
        return RespUtils.respPageInfoVO(pageInfo);
    }

    /**
     * @author: ChengJW
     * @params: [map]
     * @return: com.cheng.data.vo.NoticeVO
     * @date: 2021/12/21
     * @description: home 页面待迁入企业提醒
     */
    @Override
    public NoticeVO queryMoveNoticeVO(String userCode) throws CommonException {
        Map<String,Object> map = new HashMap<>();
        map.put(CodeUtil.USER_CODE,userCode);
        return companyMoveInfService.queryMoveNoticeVO(map);
    }
}
